package kr.hkit.summary_java.erp;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.db.DbUtil;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Department;
import kr.hkit.summary_java.erp.dto.Employee;
import kr.hkit.summary_java.serivceUI.PanelDepartmentUI;
import kr.hkit.summary_java.serivceUI.PanelEmployeeUI;
import kr.hkit.summary_java.serivceUI.TestFrame;

public class MainFrame extends JFrame implements ActionListener {

	private JPanel contentPane;
	
	private JMenuItem addItem;
	private JMenuItem deleteItem;
	private JMenuItem updateItem;
	private JMenuItem searchItem;
	private JMenuItem ItemLists;
	private JTable table_dept;
	
	private PanelDepartmentUI insertDept;
	private PanelDepartmentUI deleteDept;
	private PanelDepartmentUI updateDept;
	private PanelDepartmentUI searchDept; 
	
	private DepartmentDAO dao_dept;
	private Connection con;
	
	private JMenuItem addItem_emp;
	private JMenuItem deleteItem_emp;
	private JMenuItem updateItem_emp;
	private JMenuItem searchItem_emp;
	private JMenuItem ItemLists_emp;
	private JTable table_emp;
	
	private PanelEmployeeUI insertEmp;
	private PanelEmployeeUI deleteEmp;
	private PanelEmployeeUI updateEmp;
	private PanelEmployeeUI searchEmp;

	private EmployeeDAO dao_emp;

	private String[][] datas;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			

			public void run() {
				try {
					Connection con = ConnectionFactory.getConnection();
					MainFrame frame = new MainFrame(con);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	/**
	 * Create the frame.
	 */
	public MainFrame(Connection con) {
		setTitle("Summary_Java_Chu");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		//Department
		JMenu deptMenu = new JMenu("부서정보");
		menuBar.add(deptMenu);
		
		addItem = new JMenuItem("추가");
		addItem.addActionListener(this);
		deptMenu.add(addItem);
		
		deleteItem = new JMenuItem("삭제");
		deleteItem.addActionListener(this);
		deptMenu.add(deleteItem);
		
		updateItem = new JMenuItem("수정");
		updateItem.addActionListener(this);
		deptMenu.add(updateItem);
		
		searchItem = new JMenuItem("검색");
		searchItem.addActionListener(this);
		deptMenu.add(searchItem);
		
		ItemLists = new JMenuItem("리스트보기");
		ItemLists.addActionListener(this);
		deptMenu.add(ItemLists);
		
		//employee
		JMenu empMenu = new JMenu("사원정보");
		menuBar.add(empMenu);
		
		addItem_emp = new JMenuItem("추가");
		addItem_emp.addActionListener(this);
		empMenu.add(addItem_emp);
		
		deleteItem_emp = new JMenuItem("삭제");
		deleteItem_emp.addActionListener(this);
		empMenu.add(deleteItem_emp);
		
		updateItem_emp = new JMenuItem("수정");
		updateItem_emp.addActionListener(this);
		empMenu.add(updateItem_emp);
		
		searchItem_emp = new JMenuItem("검색");
		searchItem_emp.addActionListener(this);
		empMenu.add(searchItem_emp);
		
		ItemLists_emp = new JMenuItem("리스트보기");
		ItemLists_emp.addActionListener(this);
		empMenu.add(ItemLists_emp);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		dao_emp = new EmployeeDAO(con);
		dao_dept = new DepartmentDAO(con);
	
	}
	

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == ItemLists_emp) {
			actionPerformedItemLists_emp(e);
		}
		if (e.getSource() == searchItem_emp) {
			actionPerformedSearchItem_emp(e);
		}
		if (e.getSource() == updateItem_emp) {
			actionPerformedUpdateItem_emp(e);
		}
		if (e.getSource() == deleteItem_emp) {
			actionPerformedDeleteItem_emp(e);
		}
		if (e.getSource() == addItem_emp) {
			actionPerformedAddItem_emp(e);
		}
		if (e.getSource() == ItemLists) {
			actionPerformedItemLists(e);
		}
		if (e.getSource() == searchItem) {
			actionPerformedSearchItem(e);
		}
		if (e.getSource() == updateItem) {
			actionPerformedUpdateItem(e);
		}
		if (e.getSource() == deleteItem) {
			actionPerformedDeleteItem(e);
		}
		if (e.getSource() == addItem) {
			actionPerformedAddItem(e);
		}
	}
	
	protected void actionPerformedAddItem(ActionEvent e) { //추가
		insertDept = new PanelDepartmentUI("추가", dao_dept);
		contentPane.removeAll();
		contentPane.add(insertDept,BorderLayout.CENTER);
		revalidate();
		
	}
	
	protected void actionPerformedDeleteItem(ActionEvent e) { //삭제
		
		deleteDept = new PanelDepartmentUI("삭제",dao_dept);
		contentPane.removeAll();	
		contentPane.add(deleteDept,BorderLayout.CENTER);
		revalidate();
	}
	
	protected void actionPerformedUpdateItem(ActionEvent e) { //수정
		updateDept = new PanelDepartmentUI("수정",dao_dept);
		contentPane.removeAll();
		contentPane.add(updateDept,BorderLayout.CENTER);
		revalidate();
	}
	
	protected void actionPerformedSearchItem(ActionEvent e) { //검색
		searchDept = new PanelDepartmentUI("검색",dao_dept);
		contentPane.removeAll();
		contentPane.add(searchDept,BorderLayout.CENTER);
		revalidate();
	}
	
	protected void actionPerformedItemLists(ActionEvent e) { //리스트보기
		
		
		try {
			datas = dao_dept.listItems2();
		} catch (SQLException e1) {
			System.out.println("쿼리오류");
			e1.printStackTrace();
		}
		
		String[] colNames = {"부서번호", "부서명", "부서위치(층)"};
		
		DefaultTableModel model = new DefaultTableModel(datas, colNames);
		table_dept = new JTable(model);
		
		JScrollPane scrollPane = new JScrollPane(table_dept);
		
		contentPane.removeAll();	
		contentPane.add(scrollPane,BorderLayout.CENTER);
		revalidate();
		
		scrollPane.setViewportView(table_dept);
		
		tableCellAlginAndWidth();
		table_dept.setAutoCreateRowSorter(true);
		
		
		
		/*ArrayList<Department> list = new ArrayList<Department>();
		
		try {
			list = dao_dept.listItems();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		String[] colNames = {"부서번호", "부서명", "부서위치(층)"};
		String[][] data = new String[list.size()][colNames.length];
		for (int i = 0; i < list.size(); i++) {
			Department temp = list.get(i);
			data[i] = new String[] { temp.getDeptNo() + "", temp.getDeptName(), 
					temp.getFloor() + "" };
		}
		
		DefaultTableModel model = new DefaultTableModel(data, colNames);
		table_dept = new JTable(model);
		
		JScrollPane scrollPane = new JScrollPane(table_dept);
		
		contentPane.removeAll();	
		contentPane.add(scrollPane,BorderLayout.CENTER);
		revalidate();
		
		scrollPane.setViewportView(table_dept);
		
		tableCellAlginAndWidth();
		table_dept.setAutoCreateRowSorter(true);*/
		
		
	}
	
	private void tableCellAlginAndWidth() { //정렬용
		
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table_dept.getColumnModel(); 
		
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		cModel.getColumn(2).setCellRenderer(dtcrCenter);
		
	}
	
	
	//Employee Table!!!! 
	protected void actionPerformedAddItem_emp(ActionEvent e) { //추가
			insertEmp = new PanelEmployeeUI("추가", dao_emp);
			contentPane.removeAll();
			contentPane.add(insertEmp,BorderLayout.CENTER);
			revalidate();
	}
	
	protected void actionPerformedDeleteItem_emp(ActionEvent e) { //삭제
			deleteEmp = new PanelEmployeeUI("삭제", dao_emp);
			contentPane.removeAll();
			contentPane.add(deleteEmp,BorderLayout.CENTER);
			revalidate();
	}
	
	protected void actionPerformedUpdateItem_emp(ActionEvent e) { //수정
			updateEmp = new PanelEmployeeUI("수정", dao_emp);
			contentPane.removeAll();
			contentPane.add(updateEmp,BorderLayout.CENTER);
			revalidate();
	}
	protected void actionPerformedSearchItem_emp(ActionEvent e) { //검색
			
			searchEmp = new PanelEmployeeUI("검색", dao_emp);
			contentPane.removeAll();
			contentPane.add(searchEmp, BorderLayout.CENTER);	
			revalidate();
	}
	
	protected void actionPerformedItemLists_emp(ActionEvent e) { //리스트보기 
		
		ArrayList<Employee> listName = new ArrayList<Employee>();
		
		try {
			listName = dao_emp.getDeptList();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
			
		String[] colNames = {"사원번호", "사원명", "직책", "직속상관", "월급여", "부서명"};
		String[][] data = new String[listName.size()][colNames.length];
		DecimalFormat df = new DecimalFormat("#,###"); 
		for (int i = 0; i < listName.size(); i++) {
			Employee temp = listName.get(i);
			data[i] = new String[] { temp.getEmpNo()+"", temp.getEmpName(), 
					temp.getTitleS(), temp.getManager()+"", df.format(temp.getSalary()), 
					temp.getDeptNoS() };
		}
		
		DefaultTableModel model = new DefaultTableModel(data, colNames);
		table_emp = new JTable(model);
		JScrollPane scrollPane = new JScrollPane(table_emp);
		
		contentPane.removeAll();	
		contentPane.add(scrollPane,BorderLayout.CENTER);
		revalidate();
		
		scrollPane.setViewportView(table_emp);
		
		tableCellAlginAndWidth_emp();
		table_emp.setAutoCreateRowSorter(true);
		
	}
	
	public void tableCellAlginAndWidth_emp() { //정렬용
		
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table_emp.getColumnModel(); //컬럼의 개수얻기
		
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		cModel.getColumn(2).setCellRenderer(dtcrCenter);
		cModel.getColumn(2).setPreferredWidth(50); 
		cModel.getColumn(3).setCellRenderer(dtcrCenter);
		cModel.getColumn(4).setPreferredWidth(80); 
		cModel.getColumn(4).setCellRenderer(dtcrRight);
		cModel.getColumn(5).setCellRenderer(dtcrCenter);
		cModel.getColumn(5).setPreferredWidth(80);
		
	}
}


